package com.lins.storedisk.utils;

import com.lins.disk.common.APIException;
import com.lins.disk.common.AppCode;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

/**
 * @Author linshuo
 * @Date 2023/12/1 14:37
 */
public class MsmUtil {


    /**
     * 检查短信验证码
     * @Author: linshuo
     * @Date: 2023/4/28 9:44
     * @Param:   ver
     * @return: boolean
     **/
    public static boolean checkMsmCode(String phoneNumber, String code,
                                       RedisTemplate<String,String> redisTemplate){
        if (null == redisTemplate){
            throw new APIException(AppCode.BEAN_ERROR,"redisTemplate对象为空");
        }
        String realCode = redisTemplate.opsForValue().get(phoneNumber);
        if (!StringUtils.hasText(realCode)){
            throw new APIException(AppCode.MSMCODE_ERROR,"短信验证码失效，请重新获取");
        }
        if (!code.equals(realCode)){
            throw new APIException(AppCode.MSMCODE_ERROR,"短信验证码错误，请核对后再试");
        }
        return true;
    }


}
